package com.fmsz.physicalheadline.util;

import android.databinding.BindingAdapter;
import android.databinding.DataBindingUtil;
import android.databinding.InverseBindingListener;
import android.databinding.InverseBindingMethod;
import android.databinding.InverseBindingMethods;
import android.databinding.ViewDataBinding;
import android.media.MediaPlayer;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.GridLayout;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.SurfaceView;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.fmsz.physicalheadline.BR;
import com.fmsz.physicalheadline.R;
import com.fmsz.physicalheadline.activitys.WelcomeActivity;
import com.fmsz.physicalheadline.databinding.DynamicItemBinding;
import com.fmsz.physicalheadline.databinding.DynamicIvLargeItemBinding;
import com.fmsz.physicalheadline.entity.top.Snapshots;
import com.squareup.picasso.Picasso;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;

/**
 * Created by Administrator on 2016/12/15.s
 */
@InverseBindingMethods({
        @InverseBindingMethod(type = ViewPager.class, attribute = "currentItem", method = "getCurrentItem")
})
public class Binding {
    //加载item图片
    @BindingAdapter("loadUrl")
    public static void loadImage(ImageView view, String imageUrl) {
        String url = "http://lifeixphoto.qiniudn.com/" + imageUrl;
        Glide.with(view.getContext())
                .load(url)
                .into(view);
    }

    //加载比赛图标
    @BindingAdapter("loadIcon")
    public static void loadIcon(ImageView view, String imageUrl) {
        String url = "http://roi.skst.cn/logo/" + imageUrl;
        Picasso.with(view.getContext())
                .load(url)
                .into(view);
    }

    //
    @BindingAdapter("loadFullUrl")
    public static void loadFullUrl(ImageView view, String fullUrl) {
        Log.i("1620", "loadFullUrl: " + fullUrl);
        int index = fullUrl.indexOf("bigger/");
        String substring = fullUrl.substring(index + 7);
        Glide.with(view.getContext())
                .load("http://lifeixphoto.qiniudn.com/" + substring)
                .into(view);
    }
    //RadioGroup根据图片数据动态添加圆点
    @BindingAdapter({"childCount", "childLayout"})
    public static void loadPoint(RadioGroup radioGroup, int count, int layoutId) {
        radioGroup.removeAllViews();
        LayoutInflater inflater = LayoutInflater.from(radioGroup.getContext());
        for (int i = 0; i < count; i++) {
            View view = inflater.inflate(layoutId, radioGroup, false);
            view.setId(i);
            radioGroup.addView(view);
        }
    }
    //加载头条动态页面头像
    @BindingAdapter("loadHeadIcon")
    public static void loadHeadIcon(ImageView view,String url){
        String iconUrl = "https://oi3qwe3m4.qnssl.com/"+url;
        Picasso.with(view.getContext())
                .load(iconUrl)
                .transform(new UrlTransformation())
                .into(view);
    }
    //ViewPager设置当前页为RadioButton的id
    @BindingAdapter("currentItem")
    public static void setCurrentItem(ViewPager pager, int item) {
        pager.setCurrentItem(item);
    }

    //设置ViewPager与RadioButton的状态联动
    @BindingAdapter("currentItemAttrChanged")
    public static void setCurrentItemChanged(ViewPager pager, final InverseBindingListener listener) {
        pager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                listener.onChange();
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    //item详情页面webView加载数据
    @BindingAdapter("loadData")
    public static void LoadData(WebView webView, String data) {
        webView.loadData(data, "text/html;charset=utf-8;", "UTF-8");
    }

    private static final SimpleDateFormat SDF = new SimpleDateFormat("HH:mm");

    //格式化时间
    @BindingAdapter("formatTime")
    public static void formatTime(TextView textView, Date date) {
        if (date != null) {
            textView.setText("今天" + SDF.format(date));
        }
    }

    //头条中动态页面gridlayout添加view
    @BindingAdapter({"gridChildCount","gridChildData"})
    public static void childData(GridLayout gridLayout, int count, final Snapshots snapshots) {
        int layoutId;
        gridLayout.removeAllViews();
        if (count == 1) {
            gridLayout.setColumnCount(1);
        } else {
            gridLayout.setColumnCount(3);
        }
        for (int i = 0; i < count; i++) {
            if (count == 1) {
                layoutId = R.layout.dynamic_iv_large_item;
            } else {
                layoutId = R.layout.dynamic_iv_small_item;
            }
            View root = DataBindingUtil.inflate(LayoutInflater.from(gridLayout.getContext()), layoutId, gridLayout, false).getRoot();
            ViewDataBinding bind = DataBindingUtil.bind(root);
            bind.setVariable(BR.dynamic,snapshots);
            bind.setVariable(BR.imageUrl, snapshots.getImages().get(i));
            if (layoutId== R.layout.dynamic_iv_large_item) {
                ((DynamicIvLargeItemBinding) bind).playBtn.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(final View view) {
                        View parent = (View) view.getParent();
                        final View play_btn = parent.findViewById(R.id.play_btn);
                        play_btn.setVisibility(View.GONE);
                        final View progress = parent.findViewById(R.id.progress_video);
                        progress.setVisibility(View.VISIBLE);
                        SurfaceView surfaceView = (SurfaceView) parent.findViewById(R.id.surface);
                        final View video_image = parent.findViewById(R.id.video_image);
                        MediaPlayer player = new MediaPlayer();
                        player.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                            @Override
                            public void onPrepared(MediaPlayer mp) {
                                video_image.setVisibility(View.GONE);
                                progress.setVisibility(View.INVISIBLE);
                                mp.start();
                            }
                        });
                        player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                            @Override
                            public void onCompletion(MediaPlayer mp) {
                                video_image.setVisibility(View.VISIBLE);
                                play_btn.setVisibility(View.VISIBLE);
                            }
                        });
                        player.reset();
                        player.setDisplay(surfaceView.getHolder());
                        try {
                            player.setDataSource(snapshots.getVideo_link());
                            player.prepareAsync();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                });
            }
            gridLayout.addView(root);
        }
    }

    private static final SimpleDateFormat TO_DATE = new SimpleDateFormat("MM月dd日 EEE", Locale.getDefault());

    //时间格式化
    @BindingAdapter("date")
    public static void setDate(TextView view, Date date) {
        if (date != null) {
            view.setText(TO_DATE.format(date));
        }
    }
//    //点击跳过退出欢迎广告页面
//    @BindingAdapter("clickFinish")
//    public static void jumpFinish(LinearLayout view,int count){
//        WelcomeActivity context = (WelcomeActivity) view.getContext();
//        context.finish();
//    }
}
